<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
      <el-form-item label="场站名称" prop="chargeStationName">
        <el-input
          v-model="queryParams.chargeStationName"
          placeholder="请输入场站名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="桩名称" prop="deviceName">
        <el-input
          v-model="queryParams.deviceName"
          placeholder="请输入桩名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="枪口名称" prop="portName">
        <el-input
          v-model="queryParams.portName"
          placeholder="请输入枪口名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联订单号" prop="orderInfoCode">
        <el-input
          v-model="queryParams.orderInfoCode"
          placeholder="请输入关联订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customName">
        <el-input
          v-model="queryParams.customName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="用户手机号" prop="customPhone">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.customPhone"-->
      <!--          placeholder="请输入用户手机号"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="处理状态" prop="handleStatus">
        <el-select v-model="queryParams.handleStatus" placeholder="请选择处理状态" clearable>
          <el-option
            v-for="dict in dict.type.handle_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理时间" prop="handleTime">
        <el-date-picker clearable
                        v-model="queryParams.handleTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择处理时间" style="width: 210px">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['iotDevice:fault:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['iotDevice:fault:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['iotDevice:fault:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="faultList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="deviceFaultId" width="55"/>
      <!--<el-table-column label="场站编号" align="center" prop="chargeStationCode" />-->
      <el-table-column label="场站名称" align="center" prop="chargeStationName"/>
      <!--<el-table-column label="桩编号" align="center" prop="deviceCode" />-->
      <el-table-column label="桩名称" align="center" prop="deviceName"/>
      <!--<el-table-column label="枪口编号" align="center" prop="portId" />-->
      <el-table-column label="枪口名称" align="center" prop="portName"/>
      <el-table-column label="关联订单号" align="center" prop="orderInfoCode"/>
      <!--<el-table-column label="客户编号" align="center" prop="customCode" />-->
      <el-table-column label="客户名称" align="center" prop="customName"/>
      <!--<el-table-column label="用户手机号" align="center" prop="customPhone" />-->
      <el-table-column label="处理状态" align="center" prop="handleStatus" width="80">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.handle_status" :value="scope.row.handleStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="处理时间" align="center" prop="handleTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.handleTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="故障标签" align="center" prop="faultLabel" width="170">
        <template slot-scope="scope">
          <div v-for="item in scope.row.faultLabel ? scope.row.faultLabel.split(',') : []">
            <dict-tag :options="dict.type.fault_type" :value="item"/>
          </div>

        </template>
      </el-table-column>
      <!--<el-table-column label="其他原因" align="center" prop="faultContent"/>-->
      <el-table-column label="照片" align="center" prop="faultPhoto">
        <template slot-scope="scope">
          <!--<image-preview :src="scope.row.faultPhoto" :width="50" :height="50"/>-->
          <div v-if="scope.row.faultPhoto != null && scope.row.faultPhoto != undefined && scope.row.faultPhoto != ''
            && scope.row.faultPhoto.length > 0 " style="margin-left: 5px;width: 50px;height: 50px;display: inline-block"
               v-for="item in scope.row.faultPhoto">
            <img :src="item" style="width: 100%;height: 100%" @click="showPic(item)">
          </div>
          <div v-else>暂无照片</div>
        </template>
      </el-table-column>
      <!--<el-table-column label="备注" align="center" prop="faultRemark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-if="scope.row.handleStatus == '0'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row,'1')"
                     v-hasPermi="['iotDevice:fault:edit']"
          >处理
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row,'0')"
            v-hasPermi="['iotDevice:fault:edit']"
          >查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['iotDevice:fault:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或处理故障报修对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="场站名称" prop="chargeStationName">
          <el-input v-model="form.chargeStationCode" placeholder="请输入场站名称" readonly/>
        </el-form-item>
        <el-form-item label="桩名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入桩名称" readonly/>
        </el-form-item>
        <el-form-item label="枪口名称" prop="deviceName">
          <el-input v-model="form.portName" placeholder="请输入枪口名称" readonly/>
        </el-form-item>
        <el-form-item label="关联订单号" prop="orderInfoCode">
          <el-input v-model="form.orderInfoCode" placeholder="请输入关联订单号" readonly/>
        </el-form-item>
        <el-form-item label="客户名称" prop="customName">
          <el-input v-model="form.customName" placeholder="请输入客户名称" readonly/>
        </el-form-item>
        <el-form-item label="用户手机号" prop="customPhone">
          <el-input v-model="form.customPhone" placeholder="请输入用户手机号" readonly/>
        </el-form-item>
        <el-form-item label="处理状态">
          <el-radio-group v-model="form.handleStatus" :disabled="true">
            <el-radio
              v-for="dict in dict.type.handle_status"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="处理时间" prop="handleTime">
          <el-date-picker clearable readonly
                          v-model="form.handleTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择处理时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="故障标签">
          <el-checkbox-group v-model="form.faultLabel" :disabled="true">
            <el-checkbox
              v-for="dict in dict.type.fault_type"
              :key="dict.value"
              :label="dict.value">
              {{dict.label}}
            </el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="其他原因" prop="faultContent">
          <el-input v-model="form.faultContent" placeholder="请输入其他原因" readonly/>
        </el-form-item>
        <el-form-item label="照片" prop="faultPhoto">
          <div v-if="form.faultPhoto != null && form.faultPhoto != undefined && form.faultPhoto != ''
            && form.faultPhoto.length > 0 " style="margin-left: 5px;width: 100px;height: 100px;display: inline-block"
               v-for="(item,index) in form.faultPhoto">
            <img :src="item" style="width: 100%;height: 100%" @click="showPic(item)">
          </div>
          <div v-else>暂无照片</div>
        </el-form-item>
        <el-form-item label="备注" prop="faultRemark">
          <el-input v-model="form.faultRemark" placeholder="请输入备注" readonly/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" v-if="updateState == '1'">处 理</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="dialogVisible" title="预览" width="800" append-to-body>
      <img :src="dialogImageUrl" style="display: block; max-width: 100%; margin: 0 auto; max-height: 750px"/>
    </el-dialog>
  </div>
</template>

<script>
  import { listFault, getFault, delFault, addFault, updateFault } from '@/api/smart/subsidiary/fault.js'

  export default {
    name: 'Fault',
    dicts: ['fault_type', 'handle_status'],
    data() {
      return {
        dialogVisible: false,
        dialogImageUrl: '',
        reqFileUrl: 'https://www.sdzctech.cn',
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 故障报修表格数据
        faultList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        updateState: null,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          chargeStationName: null,
          deviceName: null,
          portName: null,
          orderInfoCode: null,
          customName: null,
          customPhone: null,
          handleStatus: null,
          handleTime: null,
          faultLabel: null,
          faultContent: null,
          faultPhoto: null,
          faultRemark: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {}
      }
    },
    created() {
      this.getList()
    },
    methods: {
      showPic(item) {
        this.dialogImageUrl = item
        this.dialogVisible = true
      },
      /** 查询故障报修列表 */
      getList() {
        this.loading = true
        listFault(this.queryParams).then(response => {
          let list = []
          response.rows.forEach(item => {
            let photo = []
            if (item.faultPhoto != null && item.faultPhoto != undefined && item.faultPhoto != '') {
              if (item.faultPhoto.indexOf(',') > -1) {
                item.faultPhoto.split(',').forEach(photoItem => {
                  photo.push(this.reqFileUrl + photoItem)
                })
              } else {
                photo.push(this.reqFileUrl + item.faultPhoto)
              }
              item.faultPhoto = photo
            }
            list.push(item)
          })
          this.faultList = list
          this.total = response.total
          this.loading = false
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          deviceFaultId: null,
          chargeStationCode: null,
          deviceCode: null,
          orderInfoCode: null,
          customCode: null,
          customPhone: null,
          createTime: null,
          handleStatus: '0',
          handleTime: null,
          faultLabel: [],
          faultContent: null,
          faultPhoto: null,
          faultRemark: null
        }
        this.resetForm('form')
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.deviceFaultId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.title = '添加故障报修'
      },
      /** 处理按钮操作 */
      handleUpdate(row, state) {
        this.updateState = state
        this.reset()
        const deviceFaultId = row.deviceFaultId || this.ids
        getFault(deviceFaultId).then(response => {
          let photo = []
          if (response.data.faultPhoto != null && response.data.faultPhoto != undefined && response.data.faultPhoto != '') {
            if (response.data.faultPhoto.indexOf(',') > -1) {
              response.data.faultPhoto.split(',').forEach(photoItem => {
                photo.push(this.reqFileUrl + photoItem)
              })
            } else {
              photo.push(this.reqFileUrl + response.data.faultPhoto)
            }
          }
          response.data.faultPhoto = photo
          this.form = response.data
          this.form.faultLabel = this.form.faultLabel.split(',')
          this.open = true
          this.title = '故障报修详情'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs['form'].validate(valid => {
          if (valid) {
            this.form.faultLabel = this.form.faultLabel.join(',')
            if (this.form.deviceFaultId != null) {
              this.form.handleStatus = '1'
              updateFault(this.form).then(response => {
                this.$modal.msgSuccess('处理成功')
                this.open = false
                this.getList()
              })
            } else {
              addFault(this.form).then(response => {
                this.$modal.msgSuccess('新增成功')
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const deviceFaultIds = row.deviceFaultId || this.ids
        this.$modal.confirm('是否确认删除ID为"' + deviceFaultIds + '"的数据项？').then(function() {
          return delFault(deviceFaultIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('iotDevice/fault/export', {
          ...this.queryParams
        }, `fault_${new Date().getTime()}.xlsx`)
      }
    }
  }
</script>
